import { Color } from "cc";
import { Util } from "../Util";
import { EPassiveSkill, SkillConfig } from "../Config/SkillConfig";
import { PlayerConfig } from "../Config/PlayerConfig";
import { EAnimationPlayMode, EBuff, EEvent, EPlayerType, ESkill, ESkillType } from "../Config/Enum";
import { ISkill, Skill } from "./Skill";
import { Core } from "../Core/Core";


export class SkillData {
    skills: Map<ESkill, Skill> = new Map(); // 使用接口类型
    passiveSkills:EPassiveSkill[] = [] // 已解锁的被动武学
    assignedPassiveSkill: EPassiveSkill | null = null; // 装备的被动武学
    skillConfig
    private playerType: EPlayerType;

    constructor(playerType: EPlayerType) {
        this.playerType = playerType;
        this.skillConfig = SkillConfig; // 初始化武学配置
        this.loadSkills();
    }
    // 加载武学
    private loadSkills() {
        const skills = PlayerConfig[this.playerType].skills;
        
        for (const skill of skills) {
            const config = SkillConfig[skill];
            if (!config) {
                console.error("武学配置不存在:", skill);
                continue;
            }

            const skillObj = new Skill(
                config.id,
                config.name,
                config.level,
                config.maxLevel || 10, // 默认最大等级为10
                config.cd,
                config.type,
                config.skillType,
                config.moveSpeed,
                config.playSpeed || 1, // 默认播放速度为1
                config.duration,
                config.ap,
                config.buffAp,
                config.effectChance,
                config.effectDuration || 0, // 默认效果持续时间为0
                config.slashColor,
                config.animationPlayMode,
                config.criticalChance,
                config.buffType,
                config.scale,
                config.numOfBullets,
                config.runeId || 0, // 默认符文ID为0
                config.damage || 0

            );
            this.skills.set(skill, skillObj);
        }

        // 初始化被动武学
        for (const passiveSkill of PlayerConfig[this.playerType].passiveSkills) {
            this.passiveSkills.push(passiveSkill);
        }
        // const passiveSkill = PlayerConfig[this.playerType].passiveSkills[0];
        // this.passiveSkills.push(passiveSkill);
        //console.log("321SkillData loadSkills this.passiveSkills:", this.passiveSkills);
    }

    // 序列化武学数据
    getPackage(): any {
        return {
            playerType: this.playerType,
            skills: Array.from(this.skills.entries()).map(([skillType, skill]) => [
                skillType,
                skill.getPackage()
            ]),
            passiveSkills: this.passiveSkills.slice(), // 序列化已解锁的被动武学
            assignedPassiveSkill: this.assignedPassiveSkill // 序列化装备的被动武学
        };
    }

    // 从序列化数据恢复武学
    setPackage(data: any): void {
        if (!data) return;
        
        this.playerType = data.playerType || this.playerType;
        this.skills = new Map();
        
        if (Array.isArray(data.skills)) {
            for (const [skillType, skillData] of data.skills) {
                const skill = new Skill(
                    0, '', 0,10, 1, ESkill.ShaolinSkill1, ESkillType.Forward, 
                    1, 1,0, 0, 0,0,0, Color.BLACK, EAnimationPlayMode.Normal, 0, EBuff.Slow
                );
                skill.setPackage(skillData);
                this.skills.set(skillType, skill);
            }
        }

                // 恢复被动技能数据
        if (Array.isArray(data.passiveSkills)) {
            this.passiveSkills = data.passiveSkills.slice();
        } else {
            // 如果没有保存的被动技能数据，使用默认配置
            this.passiveSkills = [];
            for (const passiveSkill of PlayerConfig[this.playerType].passiveSkills) {
                this.passiveSkills.push(passiveSkill);
            }
        }
        
        // 恢复装备的被动技能
        this.assignedPassiveSkill = data.assignedPassiveSkill || null;
        
    }
    upgradeSkill(skillType: ESkill): void {
        //console.log("321upgradeSkill in SkillData:", skillType);
        const skill = this.skills.get(skillType);
        if (!skill) {
            //加入skills

            console.error("武学不存在:", skillType);
            return;
        }

        // 获取当前武学的配置
        const config = SkillConfig[skillType];
        if (!config) {
            console.error("武学配置不存在:", skillType);
            return;
        }

        // 获取属性成长数组
        const growth = config.attributeGrowth || [0, 0, 0, 0, 0, 0, 0];
        
        // 升级武学属性
        skill.level += 1;
        skill.ap += growth[0];          // 增加攻击力
        skill.criticalChance += growth[1]; // 增加暴击率
        skill.effectChance += growth[2];   // 增加效果触发几率
        skill.numOfBullets += growth[3];   // 增加子弹数量
        skill.moveSpeed += growth[4];      // 增加移动速度
        skill.cd -=  growth[5];          // 减少调息时间
        skill.scale[0] += growth[6];          // 增加缩放比例
        skill.scale[1] += growth[6];          // 增加缩放比例



        // 发事件
        Core.Instance.event.emit(EEvent.SkillUpgrade);
        Core.Instance.save()


    }

    // 检查是否有武学没有选择符文
    hasSkillWithoutRune(numOfSkill): boolean {
        //for (const skill of this.skills.values()) {
        const skill = this.skills.get(numOfSkill as ESkill);
        if (skill) {
            if (skill.level >= 3 && skill.runeId === 0) {
                return true;
            }
        }

        //}
        return false;
    }



    
}